<?php
//	300 is more than decent
ini_set ( "max_execution_time", 300 );

/**
* Analytics reports class
* @author  :  drsoft.com <office@drsoft.com>
* @version :  1.0
* @date       22/02/2009
* Purpose  : Fun and fame!
* Released : Under GPL
* Requirements: curl extension loaded, PHP5+ would be nice as I haven't tested it on 4
* Usage:

	$analytics = new wb_analytics;
	$analytics->config [ 'user' ] = 'analytics username';
	$analytics->config [ 'password' ] = 'analytics password';

	$report = array
	(
		'profile'	=> 11040536,
		'from'		=> '2009-01-22',
		'to'		=> '2009-02-22',
		'report'	=> 'Dashboard'
	);
	
	echo $analytics->report ( $report );//	output is XML
*/

class WB_analytics {
	
	var $config = array
	(
		'datasource'	=> 'google_analytics',
		'user'		=> '',//	analytics username
		'password'	=> '',//	analytics password

	);

	//	connection status
	var $connected = FALSE;

	function connected ()
	{
		
//For this script to show graphics correctly, there must be a folder in the www root
//called "images/awstats"
//Download a copy of the awstats package from http://awstats.sourceforge.net/
//Within the package (version 6.6 as of writing) is the directory \awstats-6.6\wwwroot\icon\
//ftp all the folders in this directory to the images/awstats directory in the root
//of your website
//graphics on the stats page should be displayed correctly now
 
//Note: This will probably break when Jumba updates awstats to 6.6
//I'll fix it then :)

//http://linknacional.com.br:2082/awstats.pl?config=linknacional.com&ssl=&lang=pt
// Required settings
$Domain = 'linknacional.com'; // change this to your own domain
$UserId = 'linknaci';          // change this to your cpanel username
$Secret = '7tSyzb15N6';          // change this to your cpanel password
$ImageDir='images/awstats';    // location of the awstats graphics
 
// Optional settings
$DefaultLanguage = 'pt';
$HideLanguages   = true;
$HideAwstatsLogo = true;
$CustomLogo      = '';
$CustomAltTitle  = '';
$CustomUrl       = '';
 
// ------------------------------------------------------
 
$SayDomain=(isset($_GET['config'])) ?  $_GET['config'] : $Domain;
if(0==count($_GET))
    { $qs="config=$Domain&lang=$DefaultLanguage&framename=mainright"; }
else
    {
    $qs = ''; 
    foreach ($_GET as $key=>$value) 
        { $value = urlencode(stripslashes($value)); $qs.="$key=$value&"; }
    $qs=substr($qs,0,-1);
    }
 
$Secret=rawurlencode($Secret);
$Stats = file_get_contents("http://$UserId:$Secret@$Domain:2082/awstats.pl?$qs");

$Stats=str_replace('<form name="FormDateFilter"', "<center><h2>Web statistics for $SayDomain</h2></center>\r\n<form name=\"FormDateFilter\"", $Stats);
$Stats=str_replace('awstats.pl', $_SERVER['PHP_SELF'], $Stats);
if(substr($ImageDir,-1)<>'/') $ImageDir.='/';
$Stats=str_replace('/images/awstats/', $ImageDir, $Stats);
$Stats=str_replace('framename=index', 'framename=mainright', $Stats);
$Stats=str_replace('name="framename" value="index"', 'name="framename" value="mainright"', $Stats);
$Stats=str_replace('target="mainright"', '', $Stats);
if($TotalVisits) $Stats=preg_replace('/<td align="right" rowspan="2">(<a .*<\/a>).*<br \/><a .*<\/td>/Us', '<td align="right" rowspan="2">$1</td>', $Stats, 1);
if($HideLanguages) $Stats=preg_replace('/<td align="right" rowspan="2">(<a .*<\/a>).*<br \/><a .*<\/td>/Us', '<td align="right" rowspan="2">$1</td>', $Stats, 1);
if($HideAwstatsLogo or $CustomLogo<>'')
    {
    $Img="<img src=\"$CustomLogo\" border=\"0\" alt=\"$CustomAltTitle\" title=\"$CustomAltTitle\" />";
    $Logo=(''==$CustomUrl) ? $Img : "<a href=\"$CustomUrl\" target=\"_blank\">$Img</a>";
    if(''==$CustomLogo) $Logo='&nbsp;';
    $Stats=preg_replace('/<td align="right" rowspan="2">(<a .*<\/a>)/Us', '<td align="right" rowspan="2">'.$Logo, $Stats, 1);
    }
    preg_match("Visitantes únicos", $Stats, $matches);
    echo var_dump($matches);
echo $Stats;
	}
}

//END